package com.swsc.solarverse.service.sys.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swsc.solarverse.common.model.PageInfo;
import com.swsc.solarverse.common.util.BizAssert;
import com.swsc.solarverse.dao.entity.sys.CorporationStationDO;
import com.swsc.solarverse.dao.mapper.sys.CorporationStationMapper;
import com.swsc.solarverse.model.CorporationQueryBO;
import com.swsc.solarverse.model.CorporationStationBO;
import com.swsc.solarverse.service.sys.ICorporationStationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 电站信息表 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2022-02-24
 */
@Service
public class CorporationStationServiceImpl extends ServiceImpl<CorporationStationMapper, CorporationStationDO> implements ICorporationStationService {


    @Override
    public List<CorporationStationBO> listUserCorporationStations(CorporationQueryBO queryBo) {
        return baseMapper.listUserCorporationStations(queryBo);
    }

    @Override
    public List<CorporationStationBO> listRoleCorporationStations(CorporationQueryBO queryBo) {
        return baseMapper.listRoleCorporationStations(queryBo);
    }

    @Override
    public List<CorporationStationBO> listCorporationStations(CorporationQueryBO queryBo) {
        return baseMapper.listCorporationStations(queryBo);
    }

    @Override
    public CorporationStationBO getCorporationStation(Integer tenantNo, Integer organNo) {
        CorporationQueryBO queryBo = CorporationQueryBO.builder().tenantNo(tenantNo).build();
        if(null != organNo){
            queryBo.setOrganNoIn(Arrays.asList(organNo));
        }

        List<CorporationStationBO> bos = baseMapper.listCorporationStations(queryBo);
        if(CollectionUtils.isNotEmpty(bos)){
            return bos.get(0);
        }
        return null;
    }

    @Override
    public CorporationStationBO checkCorporationStation(Integer tenantNo, Integer organNo) {
        CorporationStationBO stationBo = getCorporationStation(tenantNo, organNo);
        BizAssert.isFalse(null == stationBo, "电站信息不存在");
        return stationBo;
    }

    @Override
    public PageInfo<CorporationStationBO> page(CorporationQueryBO queryBo, Integer pageNo, Integer pageSize) {
        Page<CorporationStationBO> page = baseMapper.selectCorporationStations(queryBo, new Page<>(pageNo, pageSize));
        return new PageInfo<>(page.getRecords(), page.getTotal(), pageNo, pageSize);
    }

    @Override
    public CorporationStationDO getAndCheckRawDo(Integer organNo) {
        CorporationStationDO stationDo = baseMapper.selectOne(Wrappers.lambdaQuery(CorporationStationDO.class)
                .eq(CorporationStationDO::getOrganNo, organNo));
        BizAssert.isFalse(null == stationDo, "电站信息不存在");
        return stationDo;
    }
}
